package de.cbf.cam.dsa.exchange;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import de.cbf.cam.dsa.exchange.interfaces.IDSAImporter;
import de.cbf.cam.dsa.exchange.views.ChooseImporterView;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.dsa.exchange";

	// This must be the ID from your extension point "importers"
	private static final String ImporterExtensionPoint_ID = "de.cbf.cam.dsa.exchange.importer";
	
	// The shared instance
	private static Activator plugin;
	
	private ChooseImporterView chooseImporterView;
	
	private ActionListener functionalitySelectImporter = new FunctionalitySelectImporter();

	private List<IDSAImporter> availableImporters = new ArrayList<IDSAImporter>();
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		availableImporters = findAvailableImporters();
		
	}

	private List<IDSAImporter> findAvailableImporters() {
		final List<IDSAImporter> resultList = new ArrayList<IDSAImporter>();
		//evaluating of extension point(Importer) registered extensions
		try {
			IConfigurationElement[] config = Platform.getExtensionRegistry()
					.getConfigurationElementsFor(ImporterExtensionPoint_ID);
			for (IConfigurationElement configurationElement : config) {
				final Object o = configurationElement.createExecutableExtension("class");
				if (o instanceof IDSAImporter) {
					IDSAImporter importer =(IDSAImporter) o;
					resultList.add(importer);
					//for running of a method in the registered extension class
//					ISafeRunnable runnable = new ISafeRunnable() {
//						@Override
//						public void handleException(Throwable exception) {
//							System.out.println("Exception in importer adding: "+exception.getLocalizedMessage());
//						}//
//						@Override
//						public void run() throws Exception {//							
//						}
//					};
//					SafeRunner.run(runnable);
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return resultList;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	public void setChooseImporterView(ChooseImporterView view){
		if(this.chooseImporterView!=null){
			this.chooseImporterView.deRegisterImporterSelectionListener(functionalitySelectImporter);
			if(this.chooseImporterView.equals(view))return;
		}		
		this.chooseImporterView = view;
		if(this.chooseImporterView!=null){
			chooseImporterView.fillImporterList(availableImporters);
			chooseImporterView.registerImporterSelectionListener(functionalitySelectImporter);
		}
		
	}
	
	private class FunctionalitySelectImporter implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			IDSAImporter importer = chooseImporterView.getSelectedImporter();
			importer.openView();
		}		
	}
}
